--
-- Author: shunguo.chen
-- Date: 2018-06-07 11:27:37
--
-- TheatreModel
--

local TheatreInfo = import(".TheatreInfo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
	self._mapId = GD:getTeamThirdTargetMapId(Enums.TeamTarget.theatre)
	self._posAry = nil

	self._myCurBossHp = 100
	self._myBossHpMax = 100
	self._myRestLife = 0

	-- print("Theatre.mapId", self._mapId)
end

function ClassRef:clear()
	self._theatreInfo = nil
end

function ClassRef:getNpcId()
	local npcId = GD:getTheatrePar("NpcID", true)
	return npcId
end

function ClassRef:getNpcWords(sessionId, isBossWin, isWin, myTeamName, enemyTeamName)
	local words
	local str
	local isOpening = type(isBossWin) == "nil" and sessionId == 1
	if isOpening then
		-- 开场信息
		str = GD:getTheatrePar("readyTalk", false)
		words = string.splitTwice(str)
		for i,v in ipairs(words) do
			v[1] = L(v[1])
			v[2] = tonumber(v[2])
		end

		str = GD:getTheatrePar("chooseRoleTalk", false)
		local ary = string.split(str, "|")
		ary[1] = string.format(L(ary[1]), sessionId)
		ary[2] = tonumber(ary[2])
		words[#words + 1] = ary
	else
		if isBossWin then
			-- boss赢了
			if isWin then
				str = GD:getTheatrePar("bossWinnerTalk", false)
			else
				str = GD:getTheatrePar("bossLoserTalk", false)
			end

			words = string.splitTwice(str)
			for i,v in ipairs(words) do
				v[1] = L(v[1])
				v[2] = tonumber(v[2])
			end
		else
			-- 玩家赢了
			local winTeamName, failTeamName
			if isWin then
				str = GD:getTheatrePar("winnerTalk", false)
				winTeamName = myTeamName
				failTeamName = enemyTeamName
			else
				str = GD:getTheatrePar("loserTalk", false)
				winTeamName = enemyTeamName
				failTeamName = myTeamName
			end

			words = string.splitTwice(str)
			local ary = words[1]
			ary[1] = string.format(L(ary[1]), sessionId, winTeamName)
			ary[2] = tonumber(ary[2])
			ary = words[2]
			ary[1] = string.format(L(ary[1]), failTeamName)
			ary[2] = tonumber(ary[2])
		end
	end
	return words
end

function ClassRef:getNpcCosplayWords(isPvp, sessionId)
	local words
	local str
	if isPvp then
		str = GD:getTheatrePar("PvpTalk", false)
		words = string.splitTwice(str)
		for i,v in ipairs(words) do
			v[1] = L(v[1])
			v[2] = tonumber(v[2])
		end
	else
		str = GD:getTheatrePar("chooseRoleTalk", false)
		local ary = string.split(str, "|")
		ary[1] = string.format(L(ary[1]), sessionId)
		ary[2] = tonumber(ary[2])
		words = {ary}
	end
	return words
end

-- 设置玩家站位
function ClassRef:getMemberPosAry()
	if not self._posAry then
		local str = GD:getTheatrePar("bornPos", false)
		self._posAry = table.tonumber(string.split(str, ";"))
	end
	return self._posAry
end

function ClassRef:isTheatreMap()
	-- print(debug.traceback("isTheatreMap", 1))
	-- print("TheatreModel.isTheatreMap", self._mapId, mapId)
	local mapId = MapManager:getMapId()
	return self._mapId == mapId
end

function ClassRef:isInTheatre()
	return self._theatreInfo ~= nil
end

function ClassRef:getTheatreInfo()
	return self._theatreInfo
end

function ClassRef:getTheatreTargetId()
	if self._theatreInfo then
		return self._theatreInfo:getTargetId()
	end
end

-- 歌剧院我方boss信息同步
function ClassRef:updateTheatreBossInfo(curHp, hpMax, life)
	-- print("updateTheatreBossInfo", curHp, hpMax, life)
	if not self._theatreInfo then return end
	if self._theatreInfo:isLastSession() then return end

	if self._myCurBossHp ~= curHp or self._myBossHpMax ~= hpMax then
		self._myCurBossHp = curHp
		self._myBossHpMax = hpMax

		local curPercent = 0
		if hpMax > 0 then
			curPercent = math.ceil(curHp / hpMax * 100)
		end
		local curSession = self._theatreInfo:getCurSession()
		local prePercent = curSession.myPercent
		if prePercent ~= curPercent then
			curSession:updateMyBossHp(curPercent)
			app:sendMsg("TheatreController", "onBossHpUpdate")
		end
	end

	if self._myRestLife ~= life then
		self._myRestLife = life
		self._theatreInfo:setMyTeamLife(life)
		app:sendMsg("TheatreController", "onTeamLifeUpdate")
	end
end

------------------------ receive ------------------------
-- 开始歌剧院
function ClassRef:S2C_TheatreInfo(msg)
	-- dump(msg, "S2C_TheatreInfo", 5)
	if not msg.theatreInfo then return end
	-- 服务器sessionID从0开始，在数据层面处理成丛1开始
	msg.theatreInfo.sessionID = msg.theatreInfo.sessionID + 1
	for i,v in ipairs(msg.theatreInfo.pveSessions) do
		v.sessionID = v.sessionID + 1
	end

	if not self._theatreInfo then
		self._theatreInfo = TheatreInfo.new()
		self._theatreInfo:init(msg.theatreInfo)

		self._myRestLife = self._theatreInfo:getMyTeamLife()
		app:sendMsg("TheatreController", "onStartTheatre")
	else
		self._theatreInfo:init(msg.theatreInfo)
	end
end

-- 剧场状态同步
function ClassRef:S2C_UpdateSessionState(msg)
	-- dump(msg, "S2C_UpdateSessionState", 10)
	if not self._theatreInfo then return end

	if self._theatreInfo.sessionID == msg.sessionID + 1 then
		self._theatreInfo.sessionState = msg.sessionState
		app:sendMsg("TheatreController", "onSessionStateUpdate", msg.sessionState)
	end
end

-- 对方boss血量同步
function ClassRef:S2C_UpdateBossHp(msg)
	-- dump(msg, "S2C_UpdateBossHp", 10)
	if not self._theatreInfo then return end

	local curPercent = 0
	if msg.bossMaxHP > 0 then
		curPercent = math.ceil(msg.bossNowHP / msg.bossMaxHP * 100)
	end
	local curSession = self._theatreInfo:getCurSession()
	if curSession.enemyPercent ~= curPercent then
		curSession:updateEnemyBossHp(curPercent)
		app:sendMsg("TheatreController", "onBossHpUpdate")
	end

	if self._theatreInfo:getEnemyTeamLife() ~= msg.ReviveTimes then
		self._theatreInfo:setEnemyTeamLife(msg.ReviveTimes)
		app:sendMsg("TheatreController", "onTeamLifeUpdate")
	end
end

-- 剧场pve结束
function ClassRef:S2C_SessionResult(msg)
	-- dump(msg, "S2C_SessionResult", 10)
	-- 服务器sessionID从0开始，在数据层面处理成丛1开始
	msg.sessionResult.sessionID = msg.sessionResult.sessionID + 1

	app:sendMsg("TheatreController", "onSessionResult", msg.sessionResult)
end

-- 歌剧院结束
function ClassRef:S2C_TheatreResult(msg)
	-- dump(msg, "S2C_TheatreResult", 10)

	app:sendMsg("TheatreController", "onTheatreResult", msg)
end

------------------------ request ------------------------
-- 从剧院地图回到现实地图
function ClassRef:reqBackToMap()
	-- print("TheatreModel.reqBackToMap")
	app:sendMsg("TheatreController", "closeView")
	app.session:request("pb_map.C2S_BackToLastMap", {})
end

-- 退出剧院
function ClassRef:reqEscape()
	print("TheatreModel.reqEscape")
	app.session:request("pb_theatre.C2S_TheatreEscape",{}, nil, nil,
		function(errMsg)
			-- dump(errMsg, "pb_theatre.C2S_TheatreEscape errMsg")
			self:reqBackToMap()
		end
	)
end

-- 动画结束
function ClassRef:reqActionOver()
	if not self._theatreInfo then return end
	local params = self._theatreInfo:getActionOverParams()
	-- dump(params, "reqActionOver")

	app.session:request("pb_theatre.C2S_TheatreActionOver", params, function(msg)
		-- dump(msg, "pb_theatre.C2S_TheatreActionOver")
	end)
end

return ClassRef
